<template>
  <div class="transactions">
    <div class="page-header">
      <h2 class="page-title">交易记录</h2>
      <p class="page-subtitle">查看您的所有交易历史</p>
    </div>
    
    <el-card class="transactions-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <el-icon class="header-icon"><List /></el-icon>
          <span>交易记录</span>
        </div>
      </template>
      
      <el-form :inline="true" :model="searchForm" style="margin-bottom: 20px">
        <el-form-item label="交易类型">
          <el-select v-model="searchForm.type" placeholder="全部" clearable>
            <el-option label="充值" value="deposit" />
            <el-option label="提现" value="withdraw" />
            <el-option label="转账" value="transfer" />
          </el-select>
        </el-form-item>
        <el-form-item label="币种">
          <el-select v-model="searchForm.currency" placeholder="全部" clearable>
            <el-option label="USDT" value="USDT" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadTransactions">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      
      <el-table :data="transactions" style="width: 100%">
        <el-table-column prop="txId" label="交易ID" width="200" />
        <el-table-column prop="type" label="类型" width="100">
          <template #default="{ row }">
            <el-tag :type="getTypeTagType(row.type)">
              {{ getTypeLabel(row.type) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="amount" label="金额" width="150">
          <template #default="{ row }">
            {{ formatBalance(row.amount) }} {{ row.currency }}
          </template>
        </el-table-column>
        <el-table-column prop="from" label="发送地址" width="200" />
        <el-table-column prop="to" label="接收地址" width="200" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusTagType(row.status)">
              {{ getStatusLabel(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="时间" />
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button
              type="text"
              size="small"
              @click="viewDetail(row)"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.pageSize"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="loadTransactions"
        @current-change="loadTransactions"
        style="margin-top: 20px"
      />
    </el-card>
    
    <!-- 详情对话框 -->
    <el-dialog v-model="detailVisible" title="交易详情" width="600px">
      <el-descriptions :column="2" border v-if="currentTransaction">
        <el-descriptions-item label="交易ID">
          {{ currentTransaction.txId }}
        </el-descriptions-item>
        <el-descriptions-item label="类型">
          <el-tag :type="getTypeTagType(currentTransaction.type)">
            {{ getTypeLabel(currentTransaction.type) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="金额">
          {{ formatBalance(currentTransaction.amount) }} {{ currentTransaction.currency }}
        </el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="getStatusTagType(currentTransaction.status)">
            {{ getStatusLabel(currentTransaction.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="发送地址" :span="2">
          {{ currentTransaction.from || '-' }}
        </el-descriptions-item>
        <el-descriptions-item label="接收地址" :span="2">
          {{ currentTransaction.to || '-' }}
        </el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">
          {{ currentTransaction.memo || '-' }}
        </el-descriptions-item>
        <el-descriptions-item label="创建时间" :span="2">
          {{ currentTransaction.createdAt }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { List } from '@element-plus/icons-vue'
import { walletApi } from '../api/wallet'

const searchForm = reactive({
  type: '',
  currency: ''
})

const transactions = ref([])
const pagination = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})

const detailVisible = ref(false)
const currentTransaction = ref(null)

onMounted(() => {
  loadTransactions()
})

const loadTransactions = async () => {
  try {
    const result = await walletApi.getTransactions({
      page: pagination.page,
      pageSize: pagination.pageSize,
      type: searchForm.type || undefined,
      currency: searchForm.currency || undefined
    })
    transactions.value = result.list || []
    pagination.total = result.total || 0
  } catch (error) {
    console.error('加载交易记录失败:', error)
  }
}

const resetSearch = () => {
  searchForm.type = ''
  searchForm.currency = ''
  pagination.page = 1
  loadTransactions()
}

const viewDetail = async (row: any) => {
  try {
    const detail = await walletApi.getTransactionDetail(row.txId)
    currentTransaction.value = detail
    detailVisible.value = true
  } catch (error) {
    console.error('加载交易详情失败:', error)
  }
}

const formatBalance = (value: number) => {
  return value
}

const getTypeLabel = (type: string) => {
  const map: Record<string, string> = {
    deposit: '充值',
    withdraw: '提现',
    transfer: '转账'
  }
  return map[type] || type
}

const getTypeTagType = (type: string) => {
  const map: Record<string, string> = {
    deposit: 'success',
    withdraw: 'warning',
    transfer: 'info'
  }
  return map[type] || ''
}

const getStatusLabel = (status: string) => {
  const map: Record<string, string> = {
    pending: '处理中',
    confirmed: '已确认',
    failed: '失败'
  }
  return map[status] || status
}

const getStatusTagType = (status: string) => {
  const map: Record<string, string> = {
    pending: 'warning',
    confirmed: 'success',
    failed: 'danger'
  }
  return map[status] || ''
}
</script>

<style scoped>
.transactions {
  padding: 0;
}

.page-header {
  margin-bottom: 24px;
}

.page-title {
  font-size: 28px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 8px 0;
}

.page-subtitle {
  font-size: 14px;
  color: #909399;
  margin: 0;
}

.transactions-card {
  margin-bottom: 0;
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-icon {
  font-size: 18px;
  color: #409eff;
}
</style>

